<?php

$login_url = '';

include './config.php';
if(defined('SAE_ACCESSKEY'))	//SAE平台
{
	session_start();
	//include './include/weibosae.php';
	include 'saet.ex.class.php';	

/*
	//取得授权登录URL
	$o = new SaeTOAuth( WB_AKEY , WB_SKEY  );
	$keys = $o->getRequestToken();
	$login_url = $o->getAuthorizeURL( $keys['oauth_token'] ,false , 'http://'. $_SERVER['HTTP_APPNAME'] . '.sinaapp.com' . '/callback.php');
*/	
	//取得用户计算过的数据列表
	$mc_key = 'list';
	$mc = memcache_init();
	$mc && $result = memcache_get($mc, $mc_key);

	if($result === false)
	{
		$wb_base = new SaeTClient(WB_AKEY, WB_SKEY, WB_OAUTH_TOKEN, WB_TOKEN_SECRET);
		/*$wb_base = new cls_weibosae(WB_AKEY);
		$wb_base->setUser(WB_USERNAME, WB_PASSWORD);
*/

		$i=0;
		$result = array();
		foreach($wb_base->mentions(1, 50) as $value)
		{
			if($i >= 20)
			{
				break;
			}

			if(preg_match('/^我通过\s*@互粉查询\s*计算出我的微博的互粉率是\s*(\d+)%，([^\s]+)\s*你也来看看你的互粉率吧！/s', $value['text'], $data))
			{
				if($data[1] < 0 || $data[1] > 100)
				{
					continue;
				}
				$result[$i++] = array(
					'value'		=> $data[1],
					'tips'		=> $data[2],
					'uid'		=> $value['user']['id'],
					'nick'		=> $value['user']['screen_name'],
					'domain'	=> $value['user']['domain'],
					'img'		=> $value['user']['profile_image_url'],
					'fans'		=> $value['user']['followers_count'],
					'friends'	=> $value['user']['friends_count'],
				);
			}
		}
		$result = json_encode($result);
		
		memcache_set($mc, $mc_key, $result, null, 300);
	}
}

//检查当前IP是否为内部IP
function ip_inner()
{
	if(isset($_SERVER['HTTP_VIA']))	return true;
	if(isset($_SERVER['HTTP_X_FORWARDED_FOR']))
	{
		return (substr($_SERVER['HTTP_X_FORWARDED_FOR'], 0, 10) == '61.135.152' || $_SERVER['HTTP_X_FORWARDED_FOR'] == '219.142.118.227' || substr($_SERVER['HTTP_X_FORWARDED_FOR'], 0, 3) == '10.');
	}
	return (substr($_SERVER['REMOTE_ADDR'], 0, 3) == '10.');
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>新浪微博·互粉查询</title>
<link href="css/main.css?v=15" rel="stylesheet" type="text/css"></link>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="http://js.wcdn.cn/t3/platform/js/api/wb.js" charset="utf-8" ></script>
</head>

<body>

<h1 class="center">新浪微博·互粉查询</h1>

<div id="main">

    <div id="content">
        <h2>使用说明：</h2>

        <ol>
            <li>本应用可以查询你关注嘀人当中，有哪些没有关注你，你嘀被关注率系多少。</li>
            <li>用法粉简单，直接使用新浪微博账号登录，就可以计算出来了，如果你关注嘀人比较多，可能计算时间会比较长噢！</li>
			<li>建议您没事的时候，多来看看，方能对自己的关注、粉丝有一定的了解。</li>
			<li>此应用可能在双核浏览器中<strong>不正常</strong>（如：傲游3、搜狗、360等），如果访问不正常，请尝试更换单核心浏览器（如：IE、Firefox、Chrome等）</li>
        </ol>

        <div id="login" class="center start">
            <a href="###" onclick="login();">
                <img src="images/weibo_login.png" alt="使用微博账号登录" />
            </a>
        </div>

        <div id="userinfo" class="hide center">
            <span id="nick"></span>
            <a href="###" onclick="logout();">退出</a>
            <div class="center start">
                <p class="red">
                    <label for="send_mblog"><input type="checkbox" id="send_mblog" name="send_mblog" value="1" /><b>同时发一条微博告诉大家结果</b></label>
                </p>
                <p>
                    <a href="#result" onclick="compute(this)">
                        <img src="./images/start.jpg" alt="开始计算" />
                    </a>
                </p>
            </div>
        </div>
    </div>
    <div id="dlist_block"><div id="dlist"></div></div>
    <div class="clear"></div>

	<h4 id="tips" class="hide"></h4>
	<a id="result_finish" name="result_finish"></a>
	<form method="POST" action="">
	<div id="result" class="hide">
        <h2>计算结果：</h2>
		<table border="1">
			<tr>
				<th>关注总数</th>
				<th>进度</th>
				<th>没有关注你的人数</th>
				<th>互粉率</th>
			</tr>
			<tr>
				<td id="total">读取中...</td>
				<td><span id="progress">0</span>/<span id="progress_total">读取中...</span></td>
				<td id="unfollow">0</td>
				<td id="follow_by_rate">计算中...</td>
			</tr>
		</table>

		
		<h3 id="unfolow_title">未关注的用户列表</h3>
		<ul id="unfolow_list"></ul>
		<div class="clear"></div>
		<div class="control_btn">
			<input type="button" value="全选" onclick="$(':checkbox[name=cancel]').attr('checked', true)" />
			<input type="button" value="全不选" onclick="$(':checkbox[name=cancel]').attr('checked', false)" />
			<input type="button" value="批量取消关注" onclick="cancel_follow_all()" />
			<?php if(date('m-d') == '04-01') : ?>
			<input type="button" value="强制调整互粉率为100%(强制添加粉丝)" onclick="window.open('./makefen100.php')" />
			<?php endif; ?>
		</div>
	</div>
	</form>
	
	<?php if(false && !ip_inner()) : ?>
	<div style="text-align:center;"><a target="_blank" href="http://www.weichuanbo.com/"><img src="images/rt_money.gif" alt="" /></a></div>
	<?php endif; ?>
	
	<hr />
	<div>
		<div style="float:right">
			<span style="display:none"><script src="http://s21.cnzz.com/stat.php?id=2970315&web_id=2970315&show=pic" language="JavaScript" type="text/javascript"></script></span>
			Copyright &copy; 2010 <a href="http://t.sina.com.cn/flyhope" target="_blank">@李枨煊</a>
		</div>
		<div style="float:left">
			<a href="http://sae.sina.com.cn" target="_blank"><img src="http://sae.sina.com.cn/static/image/poweredby/120X33_transparent.gif" title="Powered by Sina App Engine"></a>
		</div>
	</div>
	<div class="clear"></div>
</div>

<script type="text/javascript">var $app_key = "<?= WB_AKEY ?>";</script>
<script type="text/javascript" src="js/main.js?v=17"></script>
<div id="dlist_data" class="hide"><?= $result ?></div>
<!--<?php var_dump($_SERVER['REMOTE_ADDR']); ?>-->
</body>
</html>